// 订单路由

const express = require('express');
// 引入连接池模块
const pool = require('../pool');
// 创建路由器对象
const router = express.Router();

// 添加查询所有订单列表接口
// 查询订单列表，接收数据 page 和 pagesize
router.get("/list", (req, res, next) => {
    console.log(123)
    let { page, pagesize } = req.query
    if (!page) {
        page = 1;
    }
    if (!pagesize) {
        pagesize = 5;
    }

    page = pagesize * (page - 1);
    pagesize = Number(pagesize);
    let sql = 'select * from indent limit ?, ?;select count(*) as sum from indent';
    pool.query(sql, [page, pagesize], (err, data) => {
        if (err) {
            return next(err);
        }
        // console.log(data);
        // 总数据量
        var total = data[1][0].sum
        // console.log(total, '122');
        // 总页数
        var pageCount = Math.ceil(total / pagesize)
        // console.log(pageCount);
        // pool.query('')
        res.send({ code: 200, data, msg: "订单列表", page, pagesize, pageCount, total });
    });
});




// 通过单号查询单条订单信息
router.get('/odd', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where odd=?', [obj.odd], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过用户id查询单条订单信息
router.get('/uid', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where uid=?', [obj.uid], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    resolve('ok')
                }
                // r[index] = { ...r[index], ...res2[0] }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过门店id查询单条订单信息
router.get('/aid', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where aid=?', [obj.aid], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    resolve('ok')
                }
                // r[index] = { ...r[index], ...res2[0] }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过id查询订单
router.get('/id', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where id=?', [obj.id], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
                // r[index] = { ...r[index], ...res2[0] }
                // console.log(r[index])
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})

// 通过物流id查询订单
router.get('/gid', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where gid=?', [obj.gid], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
                // r[index] = { ...r[index], ...res2[0] }
                // console.log(r[index])
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    console.log(678)
    res.send({ res: rList })
})
// 通过发货状态查询订单
router.get('/delivery', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where delivery=?', [obj.delivery], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过购物车状态查询订单
router.get('/checked', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where checked=?', [obj.checked], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过付款状态查询订单
router.get('/payed', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where payed=?', [obj.payed], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过收货状态查询订单
router.get('/received', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where received=?', [obj.received], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过是否退款查询订单
router.get('/quit', async (req, res, next) => {
    var obj = req.query
    let rList = []
    let r = await new Promise((resolve, reject) => {
        pool.query('select * from indent where quit=?', [obj.quit], (err, r) => {
            if (err) {
                return next(err)
            }
            resolve(r)
        })
    })

    await new Promise((resolve, reject) => {
        let count = 0
        r.forEach((item, index) => {
            pool.query('select * from product where pid=?', [item.pid], (err2, res2) => {
                if (err2) {
                    next(err2)
                    return
                }
                rList.push({ ...r[index], ...res2[0] })
                count++
                if (count == r.length) {
                    // console.log(234)
                    resolve('ok')
                }
            })
        })

    })

    await new Promise((resolve, reject) => {
        let count = 0
        rList.forEach((item, index) => {
            pool.query('select * from user where uid=?', [item.uid], (err2, res2) => {
                if (err2) {
                    // console.log(err2)
                    next(err2)

                    return
                }
                rList[index] = { ...rList[index], ...res2[0] }
                count++
                if (count == rList.length) {
                    resolve('ok')
                }
            })
        })

    })
    res.send({ res: rList })
})
// 通过id修改订单信息的接口
router.post('/update', (req, res, next) => {
    var obj = req.body
    let { id, gid, uid, pid, aid, saleCount, logistics, odd, delivery, checked, payed, received, birth_date, quit } = obj
    pool.query('update indent set uid=?,gid=?, pid=?,aid=?,saleCount=?,logistics=?,odd=?,delivery=?,checked=?,payed=?,received=?, birth_date=?,quit=? where id=?', [uid, gid, pid, aid, saleCount, logistics, odd, delivery, checked, payed, received, birth_date, quit, id], (err, r) => {
        if (err) {
            // console.log(err)
            return next(err)
        }
        console.log(r)
        // 成功的结果是对象，如果对象下的changedRows值为00说明修改失败，否则修改成功
        if (r.changedRows === 0) {
            res.send({ code: 201, msg: '修改失败' })
        } else {
            res.send({ code: 200, msg: '修改成功' })
        }
    })
})

// 通过id删除订单的接口
router.post('/delete', (req, res, next) => {
    console.log(12)
    var obj = req.body
    pool.query('delete from indent where id=?', [obj.id], (err, r) => {
        if (err) {
            return next(err)
        }
        console.log(r)
        // 删除成功的结果是对象，如果对象下的affectedRows属性值为0，说明删除失败，否则删除失败
        if (r.affectedRows === 0) {
            // console.log('删除失败')
            res.send({ code: 201, msg: '删除失败' })
        } else {
            // console.log('删除成功')
            res.send({ code: 200, msg: '删除成功' })
        }
    })
})

// 新增订单接口
router.post('/add', (req, res, next) => {
    var obj = req.body
    // console.log(obj, '122');
    let { id, gid, uid, pid, aid, saleCount, logistics, odd, delivery, checked, payed, received, birth_date, quit } = obj
    console.log(saleCount);
    pool.query('insert into indent values(null,?,?,?,?,?,?,?,?,?,?,?,?,?)', [gid, uid, pid, aid, saleCount, logistics, odd, delivery, checked, payed, received, birth_date, quit], (err, r) => {
        if (err) {
            return next(err)
        }
        res.send({ code: 200, msg: '新增订单', data: r })
    })
})

// 订单是否已付款，接收参数 { id, payed }
router.post("/payment", (req, res, next) => {
    let { id, payed, saleCount } = req.body;
    
    pool.query("update indent set payed = ?, saleCount = ? where id = ?", [payed, saleCount, id], (err, r) => {
        if(err) return next(err);
        else {
            console.log(r);
            res.send({code: 200, msg: "修改成功"});
        }
    })

});

// 暴露路由器对象
module.exports = router;